package top.itmeans.yes.portal.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import top.itmeans.yes.entity.*;
import top.itmeans.yes.mapper.*;
import top.itmeans.yes.portal.domain.PortalProductDetail;
import top.itmeans.yes.portal.domain.ProductCategoryNode;
import top.itmeans.yes.portal.service.PortalProductService;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 前台订单管理Service实现类
 * @author Yiming.Cao
 */
@Service
public class PortalProductServiceImpl implements PortalProductService {
    @Resource
    private ProductMapper productMapper;
    @Resource
    private ProductCategoryMapper productCategoryMapper;
    @Resource
    private ProductAttributeMapper productAttributeMapper;
    @Resource
    private ProductAttributeValueMapper productAttributeValueMapper;
    @Resource
    private SkuStockMapper skuStockMapper;

    @Override
    public Page<Product> search(String keyword, Long productCategoryId, Integer pageNum, Integer pageSize, Integer sort) {
        Page<Product> productPage = new Page<>(pageNum,pageSize);
        LambdaQueryWrapper<Product> queryWrapper = new LambdaQueryWrapper<>();
        if (StrUtil.isNotEmpty(keyword)) {
            queryWrapper.like(Product::getName,keyword);
        }
        if (productCategoryId != null) {
            queryWrapper.eq(Product::getProductCategoryId,productCategoryId);
        }
        //1->按新品；2->按销量；3->价格从低到高；4->价格从高到低
        if (sort == 1) {
            queryWrapper.orderByDesc(Product::getNewStatus);
        } else if (sort == 2) {
            queryWrapper.orderByDesc(Product::getSale);
        } else if (sort == 3) {
            queryWrapper.orderByAsc(Product::getPrice);
        } else if (sort == 4) {
            queryWrapper.orderByDesc(Product::getPrice);
        }
        return productMapper.selectPage(productPage,queryWrapper);
    }

    @Override
    public List<ProductCategoryNode> categoryTreeList() {
        List<ProductCategory> allList = productCategoryMapper.selectList(new QueryWrapper<>());
        List<ProductCategoryNode> result = allList.stream()
                .filter(item -> item.getParentId().equals(0L))
                .map(item -> covert(item, allList))
                .collect(Collectors.toList());
        return result;
    }

    @Override
    public PortalProductDetail detail(Long id) {
        PortalProductDetail result = new PortalProductDetail();
        //获取商品信息
        Product product = productMapper.selectById(id);
        result.setProduct(product);
        //获取商品属性信息
        LambdaQueryWrapper<ProductAttribute> productAttributeQueryWrapper = new LambdaQueryWrapper<>();
        productAttributeQueryWrapper.eq(ProductAttribute::getProductAttributeCategoryId,product.getProductAttributeCategoryId());
        List<ProductAttribute> productAttributeList = productAttributeMapper.selectList(productAttributeQueryWrapper);
        result.setProductAttributeList(productAttributeList);
        //获取商品属性值信息
        if(CollUtil.isNotEmpty(productAttributeList)){
            List<Long> attributeIds = productAttributeList.stream().map(ProductAttribute::getId).collect(Collectors.toList());
            LambdaQueryWrapper<ProductAttributeValue> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(ProductAttributeValue::getProductId,product.getId());
            queryWrapper.in(ProductAttributeValue::getProductAttributeId,attributeIds);
            List<ProductAttributeValue> productAttributeValueList = productAttributeValueMapper.selectList(queryWrapper);
            result.setProductAttributeValueList(productAttributeValueList);
        }
        //获取商品SKU库存信息
        LambdaQueryWrapper<SkuStock> skuStockQueryWrapper = new LambdaQueryWrapper<>();
        skuStockQueryWrapper.eq(SkuStock::getProductId,product.getId());
        List<SkuStock> skuStockList = skuStockMapper.selectList(skuStockQueryWrapper);
        result.setSkuStockList(skuStockList);
        return result;
    }


    /**
     * 初始对象转化为节点对象
     */
    private ProductCategoryNode covert(ProductCategory item, List<ProductCategory> allList) {
        ProductCategoryNode node = new ProductCategoryNode();
        BeanUtils.copyProperties(item, node);
        List<ProductCategoryNode> children = allList.stream()
                .filter(subItem -> subItem.getParentId().equals(item.getId()))
                .map(subItem -> covert(subItem, allList)).collect(Collectors.toList());
        node.setChildren(children);
        return node;
    }
}
